/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file pmc_drv.c
 * @brief This file defines pmc driver functions
 */

/*******Includes***************************************************************/
#include "pmc_reg_access.h"
#if (CONFIG_FUNCTION_SAFE == 1U)
#include "libsafety.h"
#endif

/*******Definitions************************************************************/

/*******Variables**************************************************************/

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/

/*******************************************************************************
 *** Function Name : PMC_EnterPowerMode
 *** Description   : Enter power mode
 ******************************************************************************/
void PMC_EnterPowerMode(pmc_power_mode_t powerMode)
{
    ASSERT_PARAM(powerMode == POWER_MODE_STOP);

    volatile uint32_t mask;

#if (CONFIG_FUNCTION_SAFE == 1U)
    SAFETY_DisablePowerProtect();
#endif

    PMC_REG_SetPowerMode(PMC, powerMode);
    /* Set SLEEPDEEP bit of Cortex System Control Register */
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

    mask = System_SuspendAllInterrupts();

    /* Request Wait For Interrupt */
#if defined (__GNUC__)
    __asm volatile("wfi");
#elif defined ( __DCC__ )
    __asm(.short 0xbf30);
#else
    __asm("wfi");
#endif

#if (CONFIG_FLASH_CHIP_MULTI_MASTER == 1U)
    FLASH->MACR = 0x5A1234A5U;
#endif

    FLASH->DATAH = 0xFFFFFFFFU;
    FLASH->DATAL = 0xFFFFFFFFU;

#if defined(CPU_IM94) && (DISABLE_PCACHE == 1)
    FLASH->CFGR &= ~FLASH_CFGR_PCACHEEN_Msk;
    FLASH->CFGR &= ~FLASH_CFGR_PPREEN_Msk;
    (void)FLASH->CFGR;
    FLASH->CFGR |= FLASH_CFGR_PCACHECLR_Msk;
#endif

    System_ResumeAllInterrupts(mask);
}

/*******************************************************************************
 *** Function Name : PMC_ConfigLvd
 *** Description   : LVD level select and enable module, enable interrupt
 ******************************************************************************/
void PMC_ConfigLvd(pmc_lvd_level_t lvdLevel, bool moduleEn, bool intEn)
{
    ASSERT_PARAM((lvdLevel == LVD_LEVEL_LOW) || (lvdLevel == LVD_LEVEL_HIGH));

    LVD_REG_SetLevel(PMC, lvdLevel);
    LVD_REG_SetEnable(PMC, moduleEn);
    LVD_REG_SetIntEnable(PMC, intEn);

    if (intEn == true) {
        NVIC_EnableIRQ(LVD_IRQn);
    } else {
        NVIC_DisableIRQ(LVD_IRQn);
    }
}

/*******************************************************************************
 *** Function Name : PMC_GetFlag
 *** Description   : Get low voltage detet or wakeup flag
 ******************************************************************************/
bool PMC_GetFlag(pmc_flag_t flag)
{
    return (PMC_REG_GetFlag(PMC, flag));
}

/*******************************************************************************
 *** Function Name : PMC_ClearFlag
 *** Description   : Clear low voltage detet or wakeup flag
 ******************************************************************************/
void PMC_ClearFlag(pmc_flag_t flag)
{
    PMC_REG_ClearFlag(PMC, flag);
}

/*******************************************************************************
 *** Function Name : PMC_REG_SetWakeupEnable
 *** Description   : Set wakeup source enable
 ******************************************************************************/
void PMC_SetWakeupEnable(pmc_wakeup_source_t wakeupSource, bool enable)
{
    PMC_REG_SetWakeupEnable(PMC, wakeupSource, enable);
}

/*******************************************************************************
 *** Function Name : PMC_SetNmiIntEnable
 *** Description   : NMI interrupt enable
 ******************************************************************************/
void PMC_SetNmiIntEnable(bool enable)
{
#if CONFIG_NMI_INT_WAKEUP_ENABLE_INDEPENDENT_FEATURE
    NMI_REG_SetIntEnable(PMC, enable);
#else
    /* NMI interrupt enable and wakeup enable use same bit */
    PMC_REG_SetWakeupEnable(PMC, WAKEUP_SOURCE_NMI, enable);
#endif
}

/*******EOF********************************************************************/
